package model;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description：关于User表，涉及到的操作
 * 1.根据userName查询用户信息(实现登陆的时候)
 * 2.根据用户的id来查询用户信息（获取文章的时候，根据博客的userId 拿到作者的信息）
 * User：chenshu
 * Date：2023-12-07
 * Time：00:31
 */
public class UserDao {
    public void insert(User user) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //1.建立连接
            connection = DBUtil.getConnection();
            //2.构造SQL语句
            String sql = "insert into user values(null, ?, ?, ?, 0, 0, 0, 0)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getUsername());
            statement.setString(2, user.getPassword());
            statement.setString(3, user.getGitUrl());
            //3.执行sql
            int ret = statement.executeUpdate();
            if (ret == 1) {
                System.out.println("插入成功！");
            } else {
                System.out.println("插入失败！");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }

    public List<User> selectAll() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<User> users = new ArrayList<>();
        try {
            //1.connect
            connection = DBUtil.getConnection();
            //2.construct sql
            String sql = "select * from user";
            statement = connection.prepareStatement(sql);
            //3.execute sql
            resultSet = statement.executeQuery();
            //4.traversal result
            while (resultSet.next()) {
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setGitUrl(resultSet.getString("gitUrl"));
                user.setBlogCount(resultSet.getInt("blogCount"));
                user.setFollowingCount(resultSet.getInt("followingCount"));
                user.setFollowerCount(resultSet.getInt("followerCount"));
                user.setCollectionCount(resultSet.getInt("collectionCount"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return users;
    }
    public User selectByName(String username) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from user where username = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            resultSet  = statement.executeQuery();
            if (resultSet.next()) {
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setGitUrl(resultSet.getString("gitUrl"));
                user.setBlogCount(resultSet.getInt("blogCount"));
                user.setFollowingCount(resultSet.getInt("followingCount"));
                user.setFollowerCount(resultSet.getInt("followerCount"));
                user.setCollectionCount(resultSet.getInt("collectionCount"));
                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    public User selectById(int userId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from user where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            resultSet  = statement.executeQuery();
            if (resultSet.next()) {
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setBlogCount(resultSet.getInt("blogCount"));
                user.setFollowerCount(resultSet.getInt("followerCount"));
                user.setFollowingCount(resultSet.getInt("followingCount"));
                user.setCollectionCount(resultSet.getInt("collectionCount"));
                user.setGitUrl(resultSet.getString("gitUrl"));
                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    public void updateBlogCount(int userId, int blogCount) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "update user set blogCount = ? where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogCount);
            statement.setInt(2, userId);
            int ret = statement.executeUpdate();
            if (ret == 1) {
                System.out.println("更新成功！");
            } else {
                System.out.println("修改失败！");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }

    public void updateFollowingCount(int userId, int followingCount) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "update user set followingCount = ? where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, followingCount);
            statement.setInt(2, userId);
            int ret = statement.executeUpdate();
            if (ret == 1) {
                System.out.println("更新成功");
            } else {
                System.out.println("修改失败！");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }

    public void updateFollowerCount(int userId, int followerCount) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "update user set followerCount = ? where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, followerCount);
            statement.setInt(2, userId);
            int ret = statement.executeUpdate();
            if (ret == 1) {
                System.out.println("更新成功！");
            } else {
                System.out.println("修改失败！");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }

    public void updateCollectionCount(int userId, int collectionCount) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "update user set collectionCount = ? where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, collectionCount);
            statement.setInt(2, userId);
            int ret = statement.executeUpdate();
            if (ret == 1) {
                System.out.println("更新成功！");
            } else {
                System.out.println("修改失败！");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }

    public void deleteById(int userId) {
        Connection connection = null;
        PreparedStatement statement = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "delete from user where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,userId);
            int ret = statement.executeUpdate();
            if (ret == 1){
                System.out.println("删除成功！");
            } else {
                System.out.println("删除失败！");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }
}
